package demo16_4;

import java.util.Stack;

public class MyQueue {
    private Stack<Integer> stack1;
    private Stack<Integer> stack2;
    public MyQueue() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    public void push(int x) {
        stack1.push(x);
    }

    public int pop() {
        if (empty()) {
            return -1;
        }
        if (!stack2.empty()) {
            return stack2.pop();
        } else {
            int size = stack1.size();
            for (int i = 0; i < size; i++) {
                int temp = stack1.pop();
                stack2.push(temp);
            }
            return stack2.pop();
        }
    }

    public int peek() {
        if (empty()) {
            return -1;
        }
        if (!stack2.empty()) {
            return stack2.peek();
        } else {
            int size = stack1.size();
            for (int i = 0; i < size; i++) {
                int temp = stack1.pop();
                stack2.push(temp);
            }
            return stack2.peek();
        }
    }

    public boolean empty() {
        return stack1.empty() && stack2.empty();
    }
}
